package com.hzeport.bgs22emduty.service.impl;

import com.hzeport.bgs22emduty.entity.BaseDeptInfo;
import com.hzeport.bgs22emduty.entity.SystemModule;
import com.hzeport.bgs22emduty.outbound.ParamSmsTemplateRepository;
import com.hzeport.bgs22emduty.outbound.SystemModuleRepository;
import com.hzeport.bgs22emduty.outbound.db.SecurityLogRepositoryImpl;
import com.hzeport.bgs22emduty.outbound.db.SystemModuleRepositoryImpl;
import com.hzeport.bgs22emduty.service.SystemModuleService;
import com.hzeport.bgs22emduty.vo.SystemModule.SystemModuleVO;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2022-09-29 13:49:06
 */
@Service
@Slf4j
public class SystemModuleServiceImpl extends BaseServiceImpl<SystemModuleRepositoryImpl, SystemModule> implements SystemModuleService {


    @Resource
    private SystemModuleRepository systemModuleRepository;

    @Override
    public Object systemModule() {
        //菜单树集合
        List<SystemModuleVO> systemModuleVOList = new ArrayList<>();
//        全部菜单
        List<SystemModule> moduleList = systemModuleRepository.list();
//        找到一级菜单
        for (SystemModule systemModule : moduleList) {
            if(systemModule.getMLevel()==1){
                //根据一级菜单找到二级
                List<SystemModuleVO> Childrens=new ArrayList<>();  //二级菜单集合
                getChild(systemModule,Childrens,moduleList);//递归找到二级菜单
                SystemModuleVO systemModuleVO = new SystemModuleVO();//二级菜单对象
                systemModuleVO.setId(systemModule.getId());
                systemModuleVO.setLabel(systemModule.getFmainalias());
                systemModuleVO.setPath(systemModule.getWPath());
                systemModuleVO.setChildren(Childrens);
                systemModuleVOList.add(systemModuleVO);
            }
        }
        return  systemModuleVOList;
    }
    private void  getChild(SystemModule systemModule, List<SystemModuleVO> Childrens , List<SystemModule> moduleList ){
        List<SystemModule> myChildren=moduleList.stream().filter(i->i.getFsuperid()!=null&&i.getFsuperid().toString().equals(systemModule.getId())).collect(Collectors.toList());
        List<SystemModuleVO> myChildrenVO=new ArrayList<>();
        for (SystemModule myChild : myChildren) {
            SystemModuleVO systemModuleVO = new SystemModuleVO();
            systemModuleVO.setId(myChild.getId());
            systemModuleVO.setLabel(myChild.getFmainalias());
            systemModuleVO.setPath(myChild.getWPath());
            myChildrenVO.add(systemModuleVO);
        }
        if (!CollectionUtils.isEmpty(myChildren)){
            Childrens.addAll(myChildrenVO);
            for (SystemModule child:myChildren){
                getChild(child,Childrens,moduleList);
            }
        }else {
            return ;
        }
    }

}
